﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using csLTDMC;
using DMC300Axis_IO.Function;

namespace OCR.SystemThread
{
    public class NG_Control
    {
        public static bool NG_Flowrec = false;
        public static bool NG_OnLive = false;
        public static Thread NG_FlowThread = null;
        public static int iNG_Step = 0;

        public static bool NG_Stop = false;
        public static bool Emergency_Stop = false;

        public static string Log = "";
        //---自动运行之绪起动------------------------------------------------------------
        public static void NG_Flow_Thread()
        {
            NG_Flowrec = true;
            NG_FlowThread = new Thread(NGFlowThread);
            NG_FlowThread.IsBackground = true;
            NG_FlowThread.Start();
        }
        //-------------------------------------------------------------------------------
        //---自动运行流程----------------------------------------------------------------
        private static void NGFlowThread(object obj)
        {
            int iCount = 0;
            while (NG_Flowrec)
            {
                if (Emergency_Stop)
                {
                    iNG_Step = 0;
                    
                    Emergency_Stop = false;
                    iCount = 0;                    
                    NG_OnLive = false;
                }

                switch (iNG_Step)
                {
                    case 10: //进入等待料到讯号触发
                        {
                            //if (DIO_Thread.Di[0] == 1)
                            //{
                            //    iNG_Step = 15;                                
                            //}
                            if (IOThread_Run.Di[0] == 1)
                            {
                                iNG_Step = 15;
                            }
                            break;
                        }
                    case 11:
                        {                            
                            
                            break;
                        }
                    case 12:
                        {
                            
                            break;
                        }
                    case 13:
                        {                            
                            break;
                        }
                    case 14:
                        {                      
                            
                            break;
                        }
                    case 15://等待设置的延迟时间(可设置)
                        {
                            Thread.Sleep(DMC300_Info.NGDelay);
                            iNG_Step = 20;
                            break;
                        }
                    case 20://启动吹气的电磁阀
                        {
                            //DIO_Thread.Do[0] = 1;
                            IOThread_Run.Do[0] = 1;
                            iNG_Step = 25;
                            break;
                        }
                    case 25: //等待设置的吹气时间(可设置)
                        {
                            Thread.Sleep(DMC300_Info.NG_Action);
                            iNG_Step = 30;
                            break;
                        }
                    case 30: //关闭吹气的电磁阀
                        {
                            //DIO_Thread.Do[0] = 0;
                            IOThread_Run.Do[0] = 0;
                            Thread.Sleep(20);
                            iNG_Step = 35;    //正常流程这边应该归0或在其他状态等待中
                            break;
                        }
                    case 35: //这个是用来测试用的循环
                        {
                            if (DMC300_Info.NG_Continuous) iNG_Step = 10;
                            else iNG_Step = 0;
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                Thread.Sleep(1);
            }
        }
    }
}
